package com.chenfan.mcn.mq.producer;

import com.alibaba.fastjson.JSON;
import com.chenfan.mcn.clients.baseinfo.BaseCustomer;
import com.chenfan.mcn.clients.baseinfo.BaseInfoClient;
import com.chenfan.mcn.constant.NumberCst;
import com.chenfan.mcn.dao.IncomeContractMapper;
import com.chenfan.mcn.enums.*;
import com.chenfan.mcn.exception.McnErrorCode;
import com.chenfan.mcn.model.IncomeContract;
import com.chenfan.mcn.mq.AbstractMessageProducer;
import com.chenfan.mcn.mq.MessageProducer;
import com.chenfan.mcn.mq.message.CfChargeCommonMessage;
import com.chenfan.mcn.utils.BusinessAssert;
import com.chenfan.mcn.utils.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.Map;

/**
 * 直播合同-线下佣金结算费用
 *
 * @author wulg
 * @date 2023/6/5
 */
@Slf4j
@Component
public class LiveContactOscMessageProducer extends AbstractMessageProducer implements MessageProducer<Long> {

    @Autowired
    private IncomeContractMapper incomeContractMapper;

    @Autowired
    private BaseInfoClient baseInfoClient;

    @Override
    public void asyncSendRecordToMq(Long incomeContractId) {
        log.info("【直播合同-线下佣金结算费】开始生成费用MQ消息，incomeContractId={}", incomeContractId);
        IncomeContract incomeContract = incomeContractMapper.selectById(incomeContractId);
        BusinessAssert.notNull(incomeContract, McnErrorCode.INCOME_NOT_EXISTS);
        BusinessAssert.notNull(IncomeContractTypeEnum.isLiveContract(incomeContract.getContractType()), "非直播合同，不允许操作");
        log.info("incomeContract数据:{}", JSON.toJSONString(incomeContract));
        Map<Long, BaseCustomer> customerMap = baseInfoClient.getMcnCustomerByIds(Collections.singletonList(incomeContract.getCustomerId())).getObj();
        // if(CooperationTypeEnum.contains(incomeContract.getCooperationType(), CooperationTypeEnum.OFFLINE_STRAIGHT_COMMISSION)
        //         && CommonUtil.greaterThanZero(incomeContract.getOfflineCommissionAmount())) {
        if (CommonUtil.greaterThanZero(incomeContract.getOfflineCommissionAmount())) {
            this.doSendRecordToMq(incomeContract.getContractCode(),
                    this.buildOfflineCommissionChargeMessage(incomeContract, customerMap, incomeContract.getOfflineCommissionAmount()),
                    this.financeTopic);
        }
    }

    /**
     * 构建直播合同-线下佣金费用
     *
     * @param incomeContract
     * @param customerMap
     * @param feeAmount
     * @return
     */
    private CfChargeCommonMessage buildOfflineCommissionChargeMessage(IncomeContract incomeContract, Map<Long, BaseCustomer> customerMap, BigDecimal feeAmount) {
        CfChargeCommonMessage chargeMessage = new CfChargeCommonMessage();
        chargeMessage.setChargeType(FinanceChargeTypeEnum.OFFLINE_COMMISSION_CHARGE.getType());
        chargeMessage.setArapType(FinanceArapTypeEnum.RECEIVE.getType());
        chargeMessage.setChargeSourceCode(incomeContract.getContractCode());
        chargeMessage.setAmountPp(feeAmount);
        chargeMessage.setPricePp(feeAmount);
        chargeMessage.setBalance(customerMap.get(incomeContract.getCustomerId()).getCustomerNameC());
        chargeMessage.setFinanceEntity(incomeContract.getCompanyContractSubject());
        chargeMessage.setCreateBy(incomeContract.getCreateBy());
        chargeMessage.setCreateName(incomeContract.getCreateName());
        chargeMessage.setCreateDate(new Date());
        chargeMessage.setUpdateBy(incomeContract.getCreateBy());
        chargeMessage.setUpdateDate(chargeMessage.getCreateDate());
        chargeMessage.setUpdateName(incomeContract.getCreateName());
        chargeMessage.setTenantId(incomeContract.getTenantId());
        chargeMessage.setCompanyId(incomeContract.getCompanyId());
        chargeMessage.setSettTemplate(null);
        chargeMessage.setTaxRate(BigDecimal.ZERO);
        /**
         * version:V1.16.1: 新增【财务信息ID+结算主体类型】
         */
        if (InvoiceFormEnum.NO_NEED_INVOICE.getType().equals(incomeContract.getInvoiceForm())) {
            chargeMessage.setAccountId(null);
        } else {
            chargeMessage.setAccountId(incomeContract.getCustomerBillingId());
        }
        chargeMessage.setBalanceType(NumberCst.TWO);
        return chargeMessage;
    }
}
